package com.eps.sumi;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.eps.service.ExcelManager;
import com.eps.util.IOUtil;
import com.eps.util.SystemUtil;

@Controller
public class SvgExporterController {

	@Autowired
	private ExcelManager excel_manager;
	
	@RequestMapping(value = "/generasvg", method = RequestMethod.POST)
	protected ModelAndView generaReporte(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ServletContext application = request.getSession().getServletContext();

		String type = request.getParameter("type");
		String svg = request.getParameter("svg");
		String filename = request.getParameter("filename");

		System.out.println("type: " + type);
		System.out.println("svg: " + svg);
		System.out.println("filename: " + filename);
		System.out.println("width: " + request.getParameter("width"));

		if (filename == null && filename.equals("")) {
			filename = "chart";
		}

		String typeString = "";
		String ext = "";
		String outFile = "";
		String width = null;
		String tempName = null;

		/*
		 * try { tempName = SecurityUtil.md5(String.valueOf(System
		 * .currentTimeMillis())); } catch (NoSuchAlgorithmException e) {
		 * e.printStackTrace(); }
		 */
		tempName = "prueba";

		if (type.equals("image/png")) {
			typeString = "-m image/png";
			ext = "png";
		} else if (type.equals("image/jpeg")) {
			typeString = "-m image/jpeg";
			ext = "jpg";
		} else if (type.equals("application/pdf")) {
			typeString = "-m application/pdf";
			ext = "pdf";
		} else if (type.equals("image/svg+xml")) {
			ext = "svg";
		}

		outFile = "temp/" + tempName + "." + ext;
		if (!typeString.equals("")) {
			width = request.getParameter("width");
			if (width != null && !width.equals(""))
				width = "-w " + width;
		}

		// Generate temporary file
		File file = new File(application.getRealPath("/swf/" + tempName + ".svg"));
		IOUtil.writeToFile(file, svg);

		/*String batikPath = application
				.getRealPath("/WEB-INF/lib/batik-rasterizer.jar");
		String classPath = "-cp " + application.getRealPath("/WEB-INF/lib");
		String outFileFullPath = application.getRealPath(outFile);
		String tempFileFullPath = application.getRealPath("swf/" + tempName
				+ ".svg");*/
		
		String batikPath = "/root/batik-rasterizer/batik-rasterizer.jar";
		String classPath = "-cp /root/batik-rasterizer";
		String outFileFullPath = application.getRealPath(outFile);
		String tempFileFullPath = application.getRealPath("swf/" + tempName + ".svg");/*linux*/

		try {
			// Convert SVG file into specified type
			SystemUtil.runCommand("java", classPath, "-jar", batikPath,
					typeString, "-d", outFileFullPath, width, tempFileFullPath);

			// Show the file to user as download
			response.setHeader("Content-disposition", "attachment; filename=" + filename + "." + ext);
			response.setHeader("Content-type", type);

			ServletOutputStream out = response.getOutputStream();
			IOUtil.fileToStream(new File(outFileFullPath), out);
			out.flush();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			response.getOutputStream().print("Error on exporting");
		}
		return null;
	}
	
	@RequestMapping(value = "/generarExcel", method = RequestMethod.GET)
	public ModelAndView procesarExcel(
			 HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ArrayList ar = new ArrayList();
		
		for(int i=0;i<100;i++){
			ArrayList a = new ArrayList();
			a.add("campo1_"+i);
			a.add("campo2_"+i);
			a.add("campo3_"+i);
			a.add("campo4_"+i);
			a.add(new BigDecimal(String.valueOf(5+i)));
			ar.add(a);
		}
		ArrayList<String> a = new ArrayList<String>();
		a.add("java.lang.String");
		a.add("java.lang.String");
		a.add("java.lang.String");
		a.add("java.lang.String");
		a.add("java.lang.String");

		ar.add(a);
		a = new ArrayList<String>();
		a.add("TITULO1");
		a.add("TITULO2");
		a.add("TITULO3");
		a.add("TITULO4");
		a.add("TITULO5");
		ar.add(a);
		
		String filename ="priueba";
		response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xls" );
		response.setHeader("Content-type", "application/vnd.ms-excel");
		
		excel_manager.exportar(ar, "prueba", "prueba_exportar", response.getOutputStream());
		return null;
	}
}